package cn.chen.hd.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;

import java.io.*;

public class TestWritable {

    public static void main(String[] args) throws IOException {
        IntWritable writable = new IntWritable();
        writable.set(1232131);

        byte[] bytes = serialize(writable);

        for(int i = 0;i < bytes.length;i ++){
            System.out.println(bytes[i]);
        }

        IntWritable writable1 = new IntWritable();
        deserialize(writable1, bytes);
        System.out.println(writable1.get());
    }

    public static byte[] serialize(Writable writable) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataout = new DataOutputStream(out);
        writable.write(dataout);
        dataout.close();
        return out.toByteArray();
    }

    public static byte[] deserialize(Writable writable,byte[] bytes) throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        DataInputStream dataIn = new DataInputStream(in);
        writable.readFields(dataIn);
        dataIn.close();
        return bytes;
    }

}
